package org.system.core.controller.system;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.system.core.annotation.Log;
import org.system.core.common.PageResult;
import org.system.core.common.Result;
import org.system.core.domain.entity.SysMenu;
import org.system.core.domain.model.TreeSelect;
import org.system.core.enums.BusinessType;
import org.system.core.service.system.SysMenuService;
import org.system.core.utils.SecurityUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author baoRoot
 */
@RestController
@Api(tags = {"菜单模块"})
@RequestMapping("/system/menu")
public class SysMenuController {

    private static final Logger logger = LoggerFactory.getLogger(SysMenuController.class);

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("/list")
    @ApiOperation("获取菜单列表")
    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    public PageResult<SysMenu> list(SysMenu sysMenu) {
        logger.info("获取菜单列表: {}", sysMenu);
        List<SysMenu> list = sysMenuService.selectMenuList(sysMenu, SecurityUtils.getUserId());
        PageInfo<SysMenu> pageInfo = new PageInfo<>(list);
        return PageResult.ok(pageInfo);
    }

    @GetMapping("/{menuId}")
    @ApiOperation("根据菜单编号获取详细信息")
    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    public Result<SysMenu> getMenuInfo(@PathVariable Long menuId) {
        return Result.ok(sysMenuService.selectMenuById(menuId));
    }

    @GetMapping("/treeSelect")
    @ApiOperation("获取菜单下拉树列表")
    public Result<List<TreeSelect>> getTreeSelect(SysMenu sysMenu) {
        logger.info("获取菜单下拉树列表: {}", sysMenu);
        List<SysMenu> list = sysMenuService.selectMenuList(sysMenu, SecurityUtils.getUserId());
        return Result.ok(sysMenuService.buildMenuTreeSelect(list));
    }

    @ApiOperation("根据角色id获取对应的菜单树形列表")
    @GetMapping("/roleMenuTreeSelect/{roleId}")
    public Result<Map<String, Object>> getRoleMenuTreeSelect(@PathVariable("roleId") Long roleId) {
        logger.info("根据角色id获取对应的菜单树形列表： {}", roleId);
        List<SysMenu> menuList = sysMenuService.selectMenuList(SecurityUtils.getUserId());
        Map<String, Object> map = new HashMap<>();
        map.put("checkedKeys", sysMenuService.selectMenuListByRoleId(roleId));
        map.put("menus",sysMenuService.buildMenuTreeSelect(menuList));
        return Result.ok(map);
    }

    @PostMapping
    @ApiOperation("新增菜单")
    @PreAuthorize("@ss.hasPermi('system:menu:add')")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    public Result<String> addMenu(@Validated @RequestBody SysMenu sysMenu) {
        logger.info("新增菜单: {}", sysMenu);
        sysMenuService.insertMenu(sysMenu);
        return Result.ok();
    }

    @PutMapping
    @ApiOperation("修改菜单")
    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    public Result<String> editMenu(@Validated @RequestBody SysMenu sysMenu) {
        logger.info("修改菜单: {}", sysMenu);
        sysMenuService.updateMenu(sysMenu);
        return Result.ok();
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/{menuId}")
    @PreAuthorize("@ss.hasPermi('system:menu:remove')")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    public Result<String> deleteMenu(@PathVariable("menuId") Long menuId) {
        logger.info("删除菜单： {}", menuId);
        sysMenuService.deleteMenu(menuId);
        return Result.ok();
    }

}
